<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
session_start();
require_once 'database/profile_handler.php';
require_once 'database/photo_handler.php';
require_once 'database/wall_handler.php';
require_once 'database/follower_handler.php';


if (!isset($_SESSION['Username']) || !isset($_SESSION['Password'])) {
    header('Location: index.php');
}

if (isset($_GET['wallid']) && isset($_POST['comment_text']) && isset($_SESSION['Username'])) {
    $wall_addcomment = new wall_handler();
    $wall_addcomment->add_comment($_GET['wallid'], $_SESSION['Username'], $_POST['comment_text']);
    header('Location: homepage.php');
}

if (isset($_GET['delete_wall'])) {
    
    $wall_delcomment = new wall_handler();
    $wall_delcomment->del_wallpost($_GET['delete_wall']);
    if (isset($_GET['other'])){
        //echo $_GET['other'];
        header('Location: otherprofile.php?UserTujuan='. $_GET['other']);
    }
    else{
        header('Location: homepage.php');
    }
    
}
if (isset($_GET['posting'])) {
    $wall_delcomment = new wall_handler();
    $wall_delcomment->post_wall($_SESSION['Username'], $_SESSION['Username'], $_POST['posting_text']);
    header('Location: homepage.php');
}

if (isset($_GET['comment_wallid']) && isset($_GET['commentator']) && isset($_GET['comment'])) {
    $wall_delcomment = new wall_handler();
    $wall_delcomment->del_wall_comment($_GET['comment_wallid'], $_GET['commentator'], $_GET['comment']);

    header('Location: homepage.php');
}

$u = $_SESSION['Username'];

$pf = new profile_handler();

$pf->get_nama_lengkap($_SESSION['Username']);
$nama = $pf->result[0];

$u = $_SESSION['Username'];

$ph = new photo_handler();

//ambil profpic id
$pf->retr_details($u, 4);
$id = $pf->result[0];


//ambil url
$ph->get_url($id);


function retrieve_jumlahwall(){
    $query = "SELECT count(wall_id) as jum FROM wall_post WHERE usr_tujuan='$u'";

    $p = new profile_handler();

    $p->exec_q($query, "jum");

    $j = $p->result;

    echo $j;
    return $j;
}

$wall2 = new wall_handler();
            $wall2->retr_post_byUsr($u);
            $res2 = $wall2->result;
?>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <!--meta http-equiv="refresh" content="5" -->

        <title>Home</title>
        <script type="text/javascript" src="script/validator.js"></script>
        <script type="text/javascript" src="script/calendar.js"></script>
        <script type="text/javascript" src="script/ajax.js"></script>
        <script type="text/javascript">
            document.getElementById("wallnya").innerHTML = viewWallTiga();

            function handling_wall(a){
                var b = <?php
                        echo retrieve_jumlahwall();
                ?>
                cekJumlahWallPost(<?php echo $res2; ?>,b,<?php echo $u; ?>);
                <?php
                    $res2 = retrieve_jumlahwall();
                ?>
            }
        </script>
        <link rel="stylesheet" href="style/style.css" />
    </head>
    <body onLoad="loadPageHide()" >
<?php
if ($_GET['gagal'] == 1) {
?>
        <script type="text/javascript">alert("Operation failed");</script>
<?
}
?>

        <div class="group_content" id="title_welcome">

            <img id="profpic_welcome" src="<? echo $ph->result[0]; ?>" alt="" />
            <span id="Welcome">Hello, <?php echo $nama; ?></span>
            <form method="post" action="home.php?posting=1">
                <div id="insert_post">
                    <textarea class="textarea" id="posting_text" name="posting_text" rows="3" onFocus="if (this.value=='Post to your wall') { this.value = '';}" onBlur="if (this.value=='') { this.value = 'Post to your wall';}">Post to your wall</textarea>
                    <input type="submit" name="SubmitPosting" id="SubmitPosting" value="Submit"/>
                </div>
            </form>
        </div>
        <div class="twotable">
            <div class="group_content" id="group_wall">

                <div id="title_wall"><strong>My Wall </strong></div><br/>
                <!-- wall disini -->
                <div id="wallnya">
                    
                    
                </div>
            </div>
            <div class="group_content" id="group_follower">
                <div id="follow_group">
                    <div class="group_content" id="follower">
                        <div id="title_group">Follower</div>
<?php
                    $follower = new follower_handler();
                    $follower->retr_follower($u);
                    if ($follower->result[0] != NULL) {
                        for ($l = 0; $l < count($follower->result); $l++) {
                            $photo_handler = new photo_handler();
                            $prof_handler = new profile_handler();
                            $prof_handler->retr_details($follower->result[$l], 4);
                            $photo_handler->get_url($prof_handler->result[0]);
?><a href="otherprofile.php?UserTujuan=<?php echo $follower->result[$l]; ?>"><img src="<?php echo $photo_handler->result[0]; ?>" alt="image" id="image_follow" /></a>
                        <?php
                        }
                    } else {
                        echo "<br />";
                    }
                        ?>
                    </div>
                    <div class="group_content" id="following">
                        <div id="title_group">Following</div>
<?php
                    $follower = new follower_handler();
                    $follower->retr_following($u);
                    if ($follower->result[0] != NULL) {
                        for ($l = 0; $l < count($follower->result); $l++) {
                            $photo_handler = new photo_handler();
                            $prof_handler = new profile_handler();
                            $prof_handler->retr_details($follower->result[$l], 4);
                            $photo_handler->get_url($prof_handler->result[0]);
?><a href="otherprofile.php?UserTujuan=<?php echo $follower->result[$l]; ?>"><img src="<?php echo $photo_handler->result[0]; ?>" alt="image" id="image_follow" /></a>
                        <?php
                        }
                    } else {
                        echo "<br />";
                    }
                        ?>
                    </div>
                </div>
                <div class="clear"></div>
            </div>
        </div>
    </body>
</html>
